package com.orangelife.mobile.zoning.adapter;

import java.util.List;
import java.util.Map;

import com.curry.orangelife.mobile.R;
import com.curry.android.adapter.FatherAdapter;
import com.orangelife.mobile.zoning.activity.SelectZoningActicity;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class SelectZoningAdapter<T> extends FatherAdapter<T> {

    private List<Map<String, Object>> list;
    private Context context;
    private int type = SelectZoningActicity.WHAT_PROVINCE;

    public SelectZoningAdapter(Context context, List<Map<String, Object>> list2, int type) {
        this.list = list2;
        this.context = context;
        this.type = type;
    }

    public void setType(int type) {
        this.type = type;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.item_zoning, null);
            viewHolder.tvZoning = (TextView) convertView
                    .findViewById(R.id.tv_zoning);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        String value = getValue(position);
        viewHolder.tvZoning.setText(value);
        return convertView;
    }

    private String getValue(int position) {
        String name = "未知";
        String key = "";
        switch (type) {
            case SelectZoningActicity.WHAT_PROVINCE:
                key = "province";
                break;
            case SelectZoningActicity.WHAT_CITY:
                key = "city";
                break;
            case SelectZoningActicity.WHAT_AREA:
                key = "district";
                break;
            default:
                return name;
        }
        name = String.valueOf(list.get(position).get(key));
        return name;
    }

    class ViewHolder {
        TextView tvZoning;
    }

    @Override
    @SuppressWarnings("unchecked")
    public BaseAdapter setList(List<T> list) {
        this.list = (List<Map<String, Object>>) list;
        return SelectZoningAdapter.this;
    }
}
